//
//  STELoginViewController.m
//  ste
//
//  Created by iRDC on 6/29/13.
//  Copyright (c) 2013 Lekima. All rights reserved.
//

#import "STELoginViewController.h"
#import "STEAppDelegate.h"
#import "STEGlobal.h"
#import "STESignUpViewController.h"
#import "STESocialNetworkManager.h"
#import "RequestManager.h"
#import "STEUserModel.h"
@interface STELoginViewController ()

@end

@implementation STELoginViewController
@synthesize userIDTxt = _userIDTxt;
@synthesize passwordTxt = _passwordTxt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}
-(IBAction) createNewAccount
{
    STESignUpViewController* signUpViewController = [[STESignUpViewController alloc] initWithNibName:@"STESignUpViewController" bundle:nil];
    [((STEAppDelegate*)([UIApplication sharedApplication].delegate)).navigationController pushViewController:signUpViewController animated:YES];
}
- (void) setUserLogged: (NSArray*) data
{
    if (data) {
        [((STEAppDelegate*)([UIApplication sharedApplication].delegate)).navigationController popViewControllerAnimated:YES];
    }else{
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Oops!Somethings wrong!" message:NULL delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:NULL, nil];
        [alert show];
    }
}
-(IBAction) login
{
    if (_userIDTxt.text && ![_userIDTxt.text isEqual:@""]
        && _passwordTxt.text && ![_passwordTxt.text isEqual:@""]) {
        STEUserModel* user = [[STEUserModel alloc] init];
        user.m_password= _passwordTxt.text;
        user.m_id= _userIDTxt.text;
        [[RequestManager sharedClient] userLoggin:user andDelegate:self onEndSelector:@selector(setUserLogged:)];
    }
    
}
-(IBAction) loginByFacebook
{
    [[STESocialNetworkManager sharedSTESocialNetworkManager] logginFacebook:self];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark Delegate For Facebook login
- (void)sharerStartedSending:(SHKSharer *)sharer
{
    NSLog(@"sharerStartedSending");
}
- (void)sharerFinishedSending:(SHKSharer *)sharer
{
     NSLog(@"sharerFinishedSending");
}
- (void)sharer:(SHKSharer *)sharer failedWithError:(NSError *)error shouldRelogin:(BOOL)shouldRelogin
{
    NSLog(@"sharer");
}
- (void)sharerCancelledSending:(SHKSharer *)sharer
{
    NSLog(@"sharerCancelledSending");
}
- (void)sharerShowBadCredentialsAlert:(SHKSharer *)sharer
{
    NSLog(@"sharerShowBadCredentialsAlert");
}
- (void)sharerShowOtherAuthorizationErrorAlert:(SHKSharer *)sharer
{
    NSLog(@"sharerShowOtherAuthorizationErrorAlert");
}
- (void)sharerAuthDidFinish:(SHKSharer *)sharer success:(BOOL)success
{
    [STEGlobal saveLogginState:success];
    NSLog(@"sharerAuthDidFinish");
    if (success) {
        [((STEAppDelegate*)([UIApplication sharedApplication].delegate)).navigationController popViewControllerAnimated:YES];
    }else
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Oops!Somethings wrong!" message:NULL delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:NULL, nil];
        [alert show];
    }

}
#pragma mark turn off keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // Any additional checks to ensure you have the correct textField here.
    [textField resignFirstResponder];
    return YES;
}
@end
